home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Modules / BackSpaceModules / Source / AquariumView / Fish.m < prev    next >
Text File  |  1993-03-12  |  4KB  |  175 lines

  1. #ifndef NS3x
  2. #import "Thinker.h"
  3. #import <appkit/defaults.h>
  4. #else
  5. #import "Thinker3.h"
  6. #endif
  7. #import "Fish.h"
  8. #import "FishBrain.h"
  9. #import <appkit/Application.h>
  10. #import "RotImage.h"
  11. #import <dpsclient/wraps.h>
  12. #import <appkit/View.h>
  13. #import <stdio.h>
  14. #import <libc.h>
  15. #import <math.h>
  16. #include <sys/stat.h>
  17.  
  18. #define FISHPICTS        20    // Maximum number without recompile
  19. #define NUMBER_OF_FRAMES    1    // Need more frames eventually
  20.  
  21. @implementation Fish
  22.  
  23. - init:sender
  24. {
  25.     NXRect bufferRect;
  26.     struct stat statbuf;
  27.     char appDirectory[MAXPATHLEN], *lastSlash, fishfile[MAXPATHLEN];
  28.     aquarium = sender;
  29.  
  30.     sizeOfAquar.width = 700;
  31.     sizeOfAquar.height = 700;
  32.  
  33.     srandom(currentTimeInMs());
  34.  
  35.     do {
  36. #ifndef NS3x
  37.         strcpy(appDirectory,NXArgv[0]);
  38.         lastSlash = rindex(appDirectory,'/');
  39.         strcpy((lastSlash?lastSlash+1:appDirectory),"FishFolder");
  40.  
  41.         sprintf(fishfile,"%s/fish.%d.tiff",appDirectory,random() % FISHPICTS + 1);
  42. #else
  43.         sprintf(fishfile,"%s/FishFolder/fish.%d.tiff",[BSThinker() moduleDirectory:"Aquarium"],(int)(random() % FISHPICTS + 1));
  44. #endif
  45.  
  46.     } while(stat(fishfile,&statbuf) == -1); 
  47.         
  48.     fish = [[RotImage allocFromZone:[self zone]] initFromFile:fishfile];
  49.  
  50.     [fish getSize:&bufferRect.size];
  51.  
  52.     bufferRect.size.width /= (NUMBER_OF_FRAMES);
  53.  
  54.     WIDTH=bufferRect.size.width;
  55.     HEIGHT=bufferRect.size.height;
  56.     brain = [[FishBrain allocFromZone:[self zone]] init:&sizeOfAquar 
  57.                     fishSize:&bufferRect.size owner:fish];
  58.  
  59.     bufferRect.size.width += MAX_X_SPEED;
  60.     bufferRect.size.height += MAX_Y_SPEED;
  61.  
  62.     buffer = [[NXImage alloc] initSize:&bufferRect.size];
  63.  
  64.     if ([buffer lockFocus]) {
  65.         [self clearBuffer];    
  66.         [buffer unlockFocus];
  67.     }
  68.  
  69.     return self;
  70. }
  71.  
  72. - move:listOfFish
  73. {
  74.     NXRect    tmpRect, myRect = {0,0,0,0};
  75.     NXPoint    otherTo = {0,0};
  76.     NXRect    new;
  77.     int    x = 0;
  78.     id    tmpObject;
  79.  
  80.  
  81.     new.origin.x = [brain getXPos];
  82.     new.origin.y = [brain getYPos];
  83.     new.size.width = WIDTH + (MAX_X_SPEED/2);
  84.     new.size.height = HEIGHT + (MAX_Y_SPEED/2);
  85.  
  86.     [buffer lockFocus];
  87.     [self clearBuffer];
  88.  
  89.  
  90. /*
  91.  * The semantics that I'm using for this is that the lower indices are
  92.  * 'behind' this fish.  The one after it in the list are in 'front' of it.
  93.  */
  94.     
  95.     while((tmpObject = [listOfFish objectAt:x++]) != self) {
  96.         tmpRect = [tmpObject getRect];
  97.         if (NXIntersectsRect(&tmpRect,&new)) {
  98.             otherTo.x = tmpRect.origin.x - new.origin.x + (MAX_X_SPEED/2);
  99.             otherTo.y = tmpRect.origin.y - new.origin.y + (MAX_Y_SPEED/2);
  100.             [tmpObject simpleDraw:&otherTo];
  101.         }
  102.     }
  103.  
  104. // Draw me.
  105.     myRect.origin.x += (MAX_X_SPEED/2);
  106.     myRect.origin.y += (MAX_Y_SPEED/2);
  107.     [self simpleDraw:&myRect.origin];
  108.  
  109. // Here is where to draw other fish in front of this one.
  110.  
  111.     while((tmpObject = [listOfFish objectAt:x++]) != self && tmpObject != nil) {
  112.  
  113.         tmpRect = [tmpObject getRect];
  114.         if (NXIntersectsRect(&tmpRect,&new)) {
  115.             otherTo.x = tmpRect.origin.x - new.origin.x + (MAX_X_SPEED/2);
  116.             otherTo.y = tmpRect.origin.y - new.origin.y + (MAX_Y_SPEED/2);
  117.             [tmpObject simpleDraw:&otherTo];
  118.         }
  119.     }
  120.  
  121.     [buffer unlockFocus];
  122.  
  123.     [buffer composite:NX_COPY toPoint:&new.origin];
  124.  
  125.     old = new;
  126.     return self;
  127. }
  128.  
  129. - viewDidResize;
  130. {
  131.     NXSize imageSize;
  132.     NXRect rect;
  133.  
  134. //    old.l = old.r = old.b = old.t = fishTo.x = fishTo.y = 0;
  135.     [aquarium getFrame:&rect];
  136.     sizeOfAquar = rect.size;
  137.  
  138.     [fish getSize:&imageSize];
  139.  
  140.     [brain viewDidResize:&sizeOfAquar];
  141.  
  142.     return self;
  143. }
  144.  
  145. -(NXRect)getRect
  146. {
  147.     NXRect tmp;
  148.  
  149.     tmp = old;
  150.  
  151.     tmp.size.width += (2*MAX_X_SPEED) + 1;
  152.     tmp.size.height += (2*MAX_Y_SPEED) + 1;
  153.     return tmp;
  154. }
  155.  
  156. -simpleDraw:(NXPoint *)point
  157. {
  158.     [fish composite:NX_SOVER toPoint:point];
  159.     return self;
  160. }
  161.  
  162. -clearBuffer
  163. {
  164. // Buffer must have already have the focus locked on it for this to work.
  165.     NXRect tmp = {0,0,0,0};
  166.  
  167.     [buffer getSize:&tmp.size];
  168.     PSsetgray(NX_BLACK);
  169.     NXRectFill(&tmp);
  170.     return self;
  171. }
  172.  
  173.     
  174. @end
  175.